//
//  BaseNavigationControllerViewController.swift
//  weile
//
//  Created by LKM on 16/10/8.
//  Copyright © 2016年 LKM. All rights reserved.
//

import UIKit

class BaseNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
    }
    
    internal override class func initialize() {
        super.initialize()
        /// 设置导航栏标题
        let navBar = UINavigationBar.appearance()
        
        navBar.tintColor = R.colors.base_color//UIColor.white//直接影响到返回按钮的颜色
        navBar.titleTextAttributes = [NSForegroundColorAttributeName: R.colors._535353, NSFontAttributeName: UIFont.systemFont(ofSize: 18)]
        
    }
    
    //原本的颜色 在没透明前 因为UINavigationBar是共享的  多UINavigationController共享的
    static var backupsUINavigationBarBgColorToTransp:(barTintColor:UIColor?,backgroundColor:UIColor?,backgroundImage:UIImage?)?
    
        
    //标题全透明
    func navigationBarToTransparent(){
        
        //已经透明了
        if(BaseNavigationController.backupsUINavigationBarBgColorToTransp != nil){
            return;
        }
        let navBar = UINavigationBar.appearance()
        
        BaseNavigationController.backupsUINavigationBarBgColorToTransp = ( barTintColor:navBar.barTintColor, backgroundColor:navBar.backgroundColor,
                                                  backgroundImage:navBar.backgroundImage(for: UIBarMetrics.default))
        
        //-----标题全透明-----
        //参照 将UINavigationBar设置为全透明的两种方法http://blog.csdn.net/nsnirvana/article/details/48182867
        //        self.navigationController?.navigationBarHidden = true  //貌似直接使用这句代码就ok了
        //        navigationController?.setNavigationBarHidden(true, animated: false)//整个标题栏都会不见的
        navBar.barTintColor = nil //UIColor.black;// YMGlobalRedColor()//背景颜色
        navBar.backgroundColor = nil;
        //去掉最后一层模糊的，不然还有最后一层模模的
        navBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        
        //        navBar.alpha=0.1 这个两个没作用 ↓
        //        navBar.isTranslucent=true 这个两个没作用↑
        //-----标题全透明-end----
    }
    func navigationBarToOriginal(){
        if(BaseNavigationController.backupsUINavigationBarBgColorToTransp == nil){
            return;
        }
        let navBar = UINavigationBar.appearance()
        navBar.barTintColor = BaseNavigationController.backupsUINavigationBarBgColorToTransp!.barTintColor
        navBar.backgroundColor = BaseNavigationController.backupsUINavigationBarBgColorToTransp!.backgroundColor;
        //去掉最后一层模糊的，不然还有最后一层模模的
        navBar.setBackgroundImage(BaseNavigationController.backupsUINavigationBarBgColorToTransp!.backgroundImage, for: UIBarMetrics.default)
        
        
        BaseNavigationController.backupsUINavigationBarBgColorToTransp = nil
    }
    
    /**
     添加返回按钮到导航栏
     */
    func addBackBtnToNavBar(_ viewController:UIViewController){
        viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "btn_back"), style: .plain, target: self, action: #selector(navigationBackClick))
        viewController.navigationItem.leftBarButtonItem?.tintColor=R.colors._939393
        
    }
    
    /**
     # 统一所有控制器导航栏左上角的返回按钮
     # 让所有push进来的控制器，它的导航栏左上角的内容都一样
     # 能拦截所有的push操作
     - parameter viewController: 需要压栈的控制器
     - parameter animated:       是否动画
     */
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        /// 这时push进来的控制器viewController，不是第一个子控制器（不是根控制器）因为第一个就是要让人觉得是显示在main主页那边的，接下来显示的都是单页
        //第二个push进来时，肯定是有一个咯。所以这个判断没什么鸟用
        if viewControllers.count > 0 {
            // push 后隐藏 tabbar,底部
            viewController.hidesBottomBarWhenPushed = true
            addBackBtnToNavBar(viewController)
        }
        super.pushViewController(viewController, animated: true)
    }
    /// 返回按钮
    func navigationBackClick() {
        if UIApplication.shared.isNetworkActivityIndicatorVisible {
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
        }
        
        if viewControllers.count == 1 {
            viewControllers[0] .dismiss(animated: true, completion: nil)
            //最后一个视图没了 自己也会消失
        }else{
            
            
            popViewController(animated: true)
        }
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
